/*
 * FPagoPagar.java
 *
 * Created on 3 de septiembre de 2008, 06:02 PM
 */

package administrador;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import javax.swing.JOptionPane;

/**
 *
 * @author  daniel1
 */
public class FPagoPagar extends javax.swing.JFrame {

Conexion conexion;
int numFact;
Usuario user;
KardexInOut compraVentas;
Vent_Deudas vent_deudas;

String bandera="";

/** Creates new form FPagoPagar */
    public FPagoPagar(Conexion cEnt,Usuario user,KardexInOut compraVentas) {
        this.user=user;
        conexion=cEnt;
        this.compraVentas=compraVentas;
        
        initComponents();
        setBounds(100,100,352,450);
        setVisible(true);
        bandera="KardexInOut";
        cb_tipo.setSelectedIndex(2);
        
        cb_tipo.setEnabled(false);
        
        llenarDatos();
        
    }
    
    public FPagoPagar(Conexion cEnt,Usuario user,Vent_Deudas vent_deudas) {
        this.vent_deudas=vent_deudas;
        this.user=user;
        conexion=cEnt;
        
        initComponents();
        setBounds(100,100,352,450);
        setVisible(true);
        bandera="CreoDeuda";
        cb_tipo.removeItemAt(2);
        llenarDatos();
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        contenedor = new javax.swing.JTabbedPane();
        panel_cue = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lbl_numCta = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        txt_resp = new javax.swing.JTextField();
        lbl_fecha = new javax.swing.JLabel();
        txt_numFact = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        area = new javax.swing.JTextArea();
        bt_regCuent = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        txt_plazo = new javax.swing.JFormattedTextField();
        txt_valor = new javax.swing.JFormattedTextField();
        jLabel18 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txt_pass = new javax.swing.JPasswordField();
        txt_codigo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        lbl_proveedor = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        cb_tipo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(FPagoPagar.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        contenedor.setName("contenedor"); // NOI18N

        panel_cue.setName("panel_cue"); // NOI18N
        panel_cue.setLayout(null);

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setForeground(resourceMap.getColor("jLabel7.foreground")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N
        panel_cue.add(jLabel7);
        jLabel7.setBounds(10, 0, 94, 16);

        jLabel8.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N
        panel_cue.add(jLabel8);
        jLabel8.setBounds(10, 90, 120, 16);

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N
        panel_cue.add(jLabel9);
        jLabel9.setBounds(230, 30, 30, 20);

        lbl_numCta.setName("lbl_numCta"); // NOI18N
        panel_cue.add(lbl_numCta);
        lbl_numCta.setBounds(260, 30, 30, 20);

        jLabel12.setFont(resourceMap.getFont("jLabel12.font")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N
        panel_cue.add(jLabel12);
        jLabel12.setBounds(90, 110, 40, 16);

        jLabel13.setFont(resourceMap.getFont("jLabel13.font")); // NOI18N
        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N
        panel_cue.add(jLabel13);
        jLabel13.setBounds(40, 130, 90, 20);

        jLabel14.setFont(resourceMap.getFont("jLabel14.font")); // NOI18N
        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N
        panel_cue.add(jLabel14);
        jLabel14.setBounds(10, 30, 60, 20);

        txt_resp.setEditable(false);
        txt_resp.setName("txt_resp"); // NOI18N
        txt_resp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_respActionPerformed(evt);
            }
        });
        panel_cue.add(txt_resp);
        txt_resp.setBounds(130, 130, 170, 20);

        lbl_fecha.setName("lbl_fecha"); // NOI18N
        panel_cue.add(lbl_fecha);
        lbl_fecha.setBounds(50, 30, 60, 20);

        txt_numFact.setEditable(false);
        txt_numFact.setName("txt_numFact"); // NOI18N
        txt_numFact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_numFactActionPerformed(evt);
            }
        });
        panel_cue.add(txt_numFact);
        txt_numFact.setBounds(130, 90, 170, 20);

        jLabel11.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N
        panel_cue.add(jLabel11);
        jLabel11.setBounds(20, 220, 100, 20);

        jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N
        panel_cue.add(jLabel16);
        jLabel16.setBounds(240, 170, 40, 20);

        jLabel17.setFont(resourceMap.getFont("jLabel17.font")); // NOI18N
        jLabel17.setText(resourceMap.getString("jLabel17.text")); // NOI18N
        jLabel17.setName("jLabel17"); // NOI18N
        panel_cue.add(jLabel17);
        jLabel17.setBounds(10, 150, 120, 20);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        area.setColumns(20);
        area.setRows(5);
        area.setName("area"); // NOI18N
        jScrollPane1.setViewportView(area);

        panel_cue.add(jScrollPane1);
        jScrollPane1.setBounds(20, 240, 190, 80);

        bt_regCuent.setText(resourceMap.getString("bt_regCuent.text")); // NOI18N
        bt_regCuent.setName("bt_regCuent"); // NOI18N
        bt_regCuent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_regCuentActionPerformed(evt);
            }
        });
        panel_cue.add(bt_regCuent);
        bt_regCuent.setBounds(210, 240, 90, 50);

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        panel_cue.add(jButton4);
        jButton4.setBounds(210, 290, 90, 23);

        txt_plazo.setName("txt_plazo"); // NOI18N
        panel_cue.add(txt_plazo);
        txt_plazo.setBounds(130, 170, 100, 20);

        txt_valor.setEditable(false);
        txt_valor.setName("txt_valor"); // NOI18N
        panel_cue.add(txt_valor);
        txt_valor.setBounds(130, 110, 110, 20);

        jLabel18.setFont(resourceMap.getFont("jLabel18.font")); // NOI18N
        jLabel18.setText(resourceMap.getString("jLabel18.text")); // NOI18N
        jLabel18.setName("jLabel18"); // NOI18N
        panel_cue.add(jLabel18);
        jLabel18.setBounds(40, 190, 90, 20);

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N
        panel_cue.add(jLabel10);
        jLabel10.setBounds(240, 150, 60, 20);

        txt_pass.setName("txt_pass"); // NOI18N
        txt_pass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_passActionPerformed(evt);
            }
        });
        txt_pass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_passKeyTyped(evt);
            }
        });
        panel_cue.add(txt_pass);
        txt_pass.setBounds(130, 150, 100, 20);

        txt_codigo.setText(resourceMap.getString("txt_codigo.text")); // NOI18N
        txt_codigo.setName("txt_codigo"); // NOI18N
        panel_cue.add(txt_codigo);
        txt_codigo.setBounds(130, 70, 110, 20);

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        panel_cue.add(jLabel1);
        jLabel1.setBounds(30, 70, 110, 20);

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        panel_cue.add(jButton1);
        jButton1.setBounds(240, 70, 60, 20);

        lbl_proveedor.setText(resourceMap.getString("lbl_proveedor.text")); // NOI18N
        lbl_proveedor.setName("lbl_proveedor"); // NOI18N
        panel_cue.add(lbl_proveedor);
        lbl_proveedor.setBounds(130, 50, 170, 20);

        jLabel19.setFont(resourceMap.getFont("jLabel19.font")); // NOI18N
        jLabel19.setText(resourceMap.getString("jLabel19.text")); // NOI18N
        jLabel19.setName("jLabel19"); // NOI18N
        panel_cue.add(jLabel19);
        jLabel19.setBounds(90, 170, 40, 20);

        cb_tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Prestamos Bancario", "Ingreso de Efectivo", "Compras" }));
        cb_tipo.setName("cb_tipo"); // NOI18N
        panel_cue.add(cb_tipo);
        cb_tipo.setBounds(130, 190, 170, 20);

        contenedor.addTab(resourceMap.getString("panel_cue.TabConstraints.tabTitle"), panel_cue); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addComponent(contenedor, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(contenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txt_respActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_respActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txt_respActionPerformed

private void txt_numFactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_numFactActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txt_numFactActionPerformed

private void bt_regCuentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_regCuentActionPerformed
// TODO add your handling code here:
    //Valido que los campos esten correctamente llenados:
    String r=txt_resp.getText().toString();
    String p=txt_plazo.getText().toString();
    if(r.replaceAll(" ", "").equals("")||p.replaceAll(" ", "").equals("")){
    JOptionPane.showMessageDialog(this,"No se han llenado algunos campos importantes.","Informacion",JOptionPane.INFORMATION_MESSAGE);
    }else{
    registrar();
    }
    
    //Refresco la Ventana entrante si es el caso:
    if(bandera.equals("CreoDeuda")){
        vent_deudas.refrescar();
    }
            
}//GEN-LAST:event_bt_regCuentActionPerformed



public void registrar(){
 //Obtengo Datos de Insercion para Insercion en Cuentas:
    String codCuenta=lbl_numCta.getText();
    String codClient=txt_codigo.getText();
    String numFac=txt_numFact.getText();
    String responsa=txt_resp.getText();
    String fecha=lbl_fecha.getText();
    String  valor=txt_valor.getText();
    String cant=txt_plazo.getText();
    String obser=area.getText();
    String fecha_max=calculaFechaMax(Integer.parseInt(cant));
    
    conexion.insertarDatos("insert into cuentaspendientes values("+codCuenta+",'"+codClient+"','E"+numFac+"','"+responsa+"','"+fecha+"',"+valor+","+cant+",'"+obser+"','falta','"+fecha_max+"','xpagar');");
    
    if(bandera.equals("KardexInOut")){
    //Registro la Compra:
    compraVentas.registrarFactura(compraVentas.comprobante,"E",1);
    }
    if(bandera.equals("CreoDeuda")){
    //Registro la Compra:
    vent_deudas.refrescar();
    }
    
    this.dispose();
    
}

public String calculaFechaMax(int meses){
int diasPlazo=meses*31;
String fechaMax="";
//Crep el calendario:
Calendar c1 = Calendar.getInstance();
//Sumo los dias:
c1.add(Calendar.DATE,diasPlazo);


//Establesco el formato final
String DATE_FORMAT = "yyyy-MM-dd";
java.text.SimpleDateFormat sdf =  new java.text.SimpleDateFormat(DATE_FORMAT);
fechaMax=sdf.format(c1.getTime());		
System.out.println("La fecha maxima es:"+fechaMax);
return fechaMax;
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
    int opccion=0;
    opccion=JOptionPane.showConfirmDialog(this,"Seguro desea borrar el contenido para una entrada nueva?","Alerta",0);
    if(opccion==0){
        //llenarDatos();
        txt_plazo.setText("");
        area.setText("");
    }
}//GEN-LAST:event_jButton4ActionPerformed

private void txt_passActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_passActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txt_passActionPerformed

private void txt_passKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_passKeyTyped
// TODO add your handling code here:
//      int presiona=evt.getKeyChar();
//        if(presiona==10){
//            String clave=txt_pass.getText();
//            ResultSet resp=conexion.consultarBase("select * from usuario where clave='"+clave+"'");
//            try {
//                resp.beforeFirst();
//                resp.next();
//                String nombre=resp.getString(2);
//                txt_resp.setText(nombre);
//                
//            } catch (Exception ex) {
//                JOptionPane.showMessageDialog(this,"Hay un error en la clave","Informacion",JOptionPane.INFORMATION_MESSAGE);
//            }
//            
//        }
}//GEN-LAST:event_txt_passKeyTyped

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    BuscadorCliente bc=new BuscadorCliente(conexion,this);
}//GEN-LAST:event_jButton1ActionPerformed


public void llenarDatos(){
    if(bandera.equals("KardexInOut")){
    //Cargo el responzable de esta accion:
    txt_resp.setText(user.nombre);
    
    //Encuentro el numero de Cuenta para este cliente
    String cod_cliente=compraVentas.txt_id.getText();
    txt_codigo.setText(cod_cliente);
    ResultSet respu=conexion.consultarBase("SELECT MAX(cod_cuentasCobrar) FROM cuentaspendientes where cod_cliente like '"+cod_cliente+"';");
    int numCuenta=0;
    try {
        respu.beforeFirst();
        respu.next();
        numCuenta=respu.getInt(1);
        
    } catch (SQLException esql) {}
    numCuenta=numCuenta+1;
    lbl_numCta.setText(String.valueOf(numCuenta));
    //Saco la fecha:
    String fecha=new java.sql.Date(System.currentTimeMillis()).toString();
    lbl_fecha.setText(fecha);
    
    
    //Numero Factura:
    String numFact=compraVentas.lbl_fact.getText();
    this.txt_numFact.setText(numFact);
    //Saco valor de la compra
    txt_valor.setText(compraVentas.txt_tot.getText());
    }
    
    
    if(bandera.equals("CreoDeuda")){
    //Cargo el responzable de esta accion:
    txt_resp.setText(user.nombre);
    
    //Saco la fecha:
    String fecha=new java.sql.Date(System.currentTimeMillis()).toString();
    lbl_fecha.setText(fecha);
        
    //Numero Factura:
    this.txt_numFact.setEditable(true);
    //Saco valor de la compra
    txt_valor.setEditable(true);
    
    }
    
}


public void sacoCuentaCliente(){
//Encuentro el numero de Cuenta para este cliente
    String cod_cliente=txt_codigo.getText();
    txt_codigo.setText(cod_cliente);
    ResultSet respu=conexion.consultarBase("SELECT MAX(cod_cuentasCobrar) FROM cuentaspendientes where cod_cliente like '"+cod_cliente+"';");
    ResultSet respu2=conexion.consultarBase("SELECT nombre FROM cliente where cod_cliente="+cod_cliente);
    
    int numCuenta=0;
    try {
        respu.beforeFirst();
        respu.next();
        numCuenta=respu.getInt(1);
        
        //Pongo el nombre del cliente:
        respu2.beforeFirst();
        respu2.next();
        lbl_proveedor.setText(respu2.getString(1));
    } catch (SQLException esql) {}
    numCuenta=numCuenta+1;
    lbl_numCta.setText(String.valueOf(numCuenta));

}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea area;
    private javax.swing.JButton bt_regCuent;
    private javax.swing.JComboBox cb_tipo;
    private javax.swing.JTabbedPane contenedor;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl_fecha;
    private javax.swing.JLabel lbl_numCta;
    private javax.swing.JLabel lbl_proveedor;
    private javax.swing.JPanel panel_cue;
    public javax.swing.JTextField txt_codigo;
    private javax.swing.JTextField txt_numFact;
    private javax.swing.JPasswordField txt_pass;
    private javax.swing.JFormattedTextField txt_plazo;
    private javax.swing.JTextField txt_resp;
    private javax.swing.JFormattedTextField txt_valor;
    // End of variables declaration//GEN-END:variables

}
